package com.logic.landseaserver.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.AnswerBasic;
import com.logic.landseaserver.domain.AnswerDetail;
import com.logic.landseaserver.persistence.read.AnswerDetailReadMapper;
import com.logic.landseaserver.persistence.write.AnswerBasicWriteMapper;
import com.logic.landseaserver.persistence.write.AnswerDetailWriteMapper;
import com.logic.landseaserver.service.IAnswerService;
import com.logic.landseaserver.ws.dto.AnswerDTO;
import com.logic.landseaserver.ws.dto.AnswerDetailDTO;

@Service
public class AnswerServiceImpl implements IAnswerService {

	@Autowired
	private AnswerBasicWriteMapper answerBasicWriteMapper;
	
	@Autowired
	private AnswerDetailWriteMapper answerDetailWriteMapper;
	
	@Autowired
	private AnswerDetailReadMapper answerDetailReadMapper;
	
	@Override
	public void delete(String arg0) throws BusinessException {
		// TODO Auto-generated method stub

	}

	@Override
	public Object get(Integer arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insert(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<?> query(QueryUtil arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub

	}

	@Override
	public Boolean validate(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean validateForDelete(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean validateForInsert(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean validateForUpdate(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertAnswer(AnswerDTO answerDTO) {
		AnswerBasic answerBasic=new AnswerBasic();
		answerBasic.setOrderId(answerDTO.getOrderId());
		answerBasic.setSurveyId(answerDTO.getSurveyId());
		if(answerDTO.getUserId()!=null){
			answerBasic.setCreatedBy(answerDTO.getUserId());
			answerBasic.setCreatedDtm(new Date());
		}
		answerBasicWriteMapper.insertSelective(answerBasic);
		int answerBasicId=answerBasic.getId();
		for(int i=0;i<answerDTO.getAnswerDetailDTO().size();i++){
			AnswerDetail answerDetail=new AnswerDetail();
			answerDetail.setAnswerBasicId(answerBasicId);
			answerDetail.setQuestionId(answerDTO.getAnswerDetailDTO().get(i).getQuestionId());
			answerDetail.setAnswer(answerDTO.getAnswerDetailDTO().get(i).getAnswer());
			if(answerDTO.getUserId()!=null){
				answerDetail.setCreatedBy(answerDTO.getUserId());
				answerDetail.setCreatedDtm(new Date());
			}
			answerDetailWriteMapper.insertSelective(answerDetail);
		}
	}

	@Override
	public List<AnswerDetailDTO> selectAnswersByOrderId(Integer id) {		
		return answerDetailReadMapper.selectAnswersByOrderId(id);
	}

	@Override
	public void editAnswers(AnswerDTO answerDTO) {
		if(answerDTO.getAnswerDetailDTO().size()>0){
			
			if(answerDTO.getOrderId()!=null){ //如果没有orderid就啥也别谈了，生成预约的时候有问题
				//获取basicId
				Integer basicId = answerDetailReadMapper.getAnswserBasicId(answerDTO.getOrderId());
				List<AnswerDetailDTO> answerDetailDTOList=answerDTO.getAnswerDetailDTO();
				if(basicId==null){
					insertAnswer(answerDTO);
				}else{
					for(int i=0;i<answerDetailDTOList.size();i++){
						AnswerDetail answerDetail=new AnswerDetail();
						AnswerDetailDTO answser = answerDetailDTOList.get(i);
						if(answser.getId()!=null){  //更新
							answerDetail.setLastModifiedBy(SecurityUtils.getCurrentLogin().getUserId());
							answerDetail.setLastModifiedDtm(new Timestamp(new Date().getTime()));
							setAnswerDetailByAnswerDetailDTO(answerDetail,answser);
							answerDetailWriteMapper.updateByPrimaryKeySelective(answerDetail);
						}else{   					//插入
							answerDetail.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
							answerDetail.setAnswerBasicId(basicId);
							setAnswerDetailByAnswerDetailDTO(answerDetail,answser);
							answerDetailWriteMapper.insertSelective(answerDetail);
						}
						
					}
				}
			}
		}		
	}

	//通过answerDetailDTO创建answerDetail
	public void setAnswerDetailByAnswerDetailDTO(AnswerDetail  answerDetail,AnswerDetailDTO answerDetailDTO){
		if(!StringUtils.isEmpty(answerDetailDTO.getId()))
			answerDetail.setId(answerDetailDTO.getId());
		if(!StringUtils.isEmpty(answerDetailDTO.getQuestionId()))
			answerDetail.setQuestionId(answerDetailDTO.getQuestionId());
		if(!StringUtils.isEmpty(answerDetailDTO.getAnswer()))
			answerDetail.setAnswer(answerDetailDTO.getAnswer());
	}


	
}
